package com.suncaper.han_yi_system.controller;

import com.suncaper.han_yi_system.pojo.entity.Menu;
import com.suncaper.han_yi_system.result.Result;
import com.suncaper.han_yi_system.service.MenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/menu")
@CrossOrigin(origins = "*")
@Tag(name = "菜单管理")
@Slf4j
public class MenuController {

    @Autowired
    MenuService menuService;

    @Operation(summary = "新增菜单信息")
    @PostMapping("/save")
    public Result save(@RequestBody Menu menu){
        menu.setCreateTime(LocalDateTime.now());
        menuService.save(menu);
        return Result.ok();
    }

    @Operation(summary = "更新菜单信息")
    @PutMapping("/update")
    public Result update(@RequestBody Menu menu){
        menu.setUpdateTime(LocalDateTime.now());
        menuService.updateById(menu);
        return Result.ok();
    }

    @Operation(summary = "删除菜单信息")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        menuService.removeById(id);
        return Result.ok();
    }

    @Operation(summary = "根据id获取菜单信息")
    @GetMapping("/getById/{id}")
    public Result getById(@PathVariable Long id){
        if(Objects.isNull(id)){
            return Result.error().message("id不能为空");
        }
        return Result.ok().data("menu", menuService.getById(id));
    }

    @Operation(summary = "获取菜单列表")
    @RequestMapping("/list")
    public Result getList(){
        return Result.ok().data("menuList", menuService.list());
    }

    @Operation(summary = "获取当前用户菜单")
    @GetMapping("/getMenuByUser")
    public Result getMenuByUser(){
        List<Menu> menuByUser = menuService.getMenuByUser();
        System.out.println("1");
        return Result.ok().data("menuList", menuByUser);
    }
}
